package pl.plesken.swt;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import pl.plesken.languages.SetLanguage;
import pl.plesken.tools.GUIConfig;
import pl.plesken.xml.ConfigParser;

public class GUIPathConfig extends Dialog {
	private static ConfigParser cnf = new ConfigParser();
	private static File GUIPath = new File("conf/GUI.xml"); //$NON-NLS-1$
	private Text pathText;
	protected Object result;
	protected Shell shell;
	StringBuffer path = new StringBuffer();
	StringBuffer lang = new StringBuffer();


	public StringBuffer getPath() {
		return path;
	}

	public void setPath(StringBuffer path) {
		this.path = path;
	}

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public GUIPathConfig(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public GUIPathConfig(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		if (GUIPath.exists()) {
			lang.append(cnf.readConf("language")); //$NON-NLS-1$
		} else {
			lang.append("English"); //$NON-NLS-1$
		}

		SetLanguage.setLang(lang.toString());
		
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLayout(new GridLayout());
		shell.setSize(363, 126);
		shell.setText(SetLanguage.getString("GUIPathConfig.info")); //$NON-NLS-1$

		
		final Group guiGroup = new Group(shell, SWT.NONE);
		guiGroup.setText(SetLanguage.getString("GUIPathConfig.pathGUI")); //$NON-NLS-1$
		final GridData gd_guiGroup = new GridData(SWT.FILL, SWT.FILL, true, true);
		guiGroup.setLayoutData(gd_guiGroup);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		guiGroup.setLayout(gridLayout);

		pathText = new Text(guiGroup, SWT.BORDER);
		final GridData gd_pathText = new GridData(SWT.FILL, SWT.FILL, true, false);
		gd_pathText.heightHint = 12;
		pathText.setLayoutData(gd_pathText);

		final Button searchGuiButton = new Button(guiGroup, SWT.NONE);
		searchGuiButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				choose();
			}
		});
		searchGuiButton.setLayoutData(new GridData(78, SWT.DEFAULT));
		searchGuiButton.setText(SetLanguage.getString("GUIPathConfig.selectPath"));  //$NON-NLS-1$

		final Button guiButton = new Button(shell, SWT.NONE);
		guiButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				doit();
			}
		});
		final GridData gd_guiButton = new GridData(SWT.FILL, SWT.CENTER, false, false);
		guiButton.setLayoutData(gd_guiButton);
		guiButton.setText("OK"); //$NON-NLS-1$
		//
	}

	private void choose() {
		try {
			DirectoryDialog Dir = new DirectoryDialog(shell, SWT.OPEN);
			String dir = Dir.open();
			pathText.setText(dir);
			path.setLength(0);
			path.append(dir);
		} catch(java.lang.IllegalArgumentException e) {	
		}
	}
	
	private void doit() {
		GUIConfig gui = new GUIConfig();
		
		if (GUIPath.exists()) {
			lang.setLength(0);
			lang.append(cnf.readConf("language")); //$NON-NLS-1$
		} else {
			lang.setLength(0);
			lang.append("English"); //$NON-NLS-1$
		}
		
		gui.setGUIPath(path.toString());
		gui.setLang(lang.toString());
		
		gui.createGUIConf();
		
		shell.close();
	}
}
